package com.timi_licai.timi_licai_account.fragment;


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.adapter.KeyLineFragmentAdapter;
import com.timi_licai.timi_licai_account.adapter.KeyLineListViewAdapter;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.model.ConsumeDetails;
import com.timi_licai.timi_licai_account.model.KeyLineListViewModel;
import com.timi_licai.timi_licai_account.utils.DatabaseQueryUtils;
import com.timi_licai.timi_licai_account.utils.DateUtils;
import com.timi_licai.timi_licai_account.utils.KeyLineUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class KeyLineFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private TextView showDate;
    private ViewPager showLinePager;
    private ListView showListView;
    private List<ConsumeDetails> detailsList;//用于查询选中的日期的查询出来的信息
    private List<KeyLineListViewModel> keyLineModelList;
    private KeyLineListViewAdapter listViewAdapter;
    private KeyLineFragmentAdapter showLinePagerAdapter;

    public KeyLineFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_key_line, container, false);
        showDate = (TextView) view.findViewById(R.id.line_title);
        showLinePager = (ViewPager) view.findViewById(R.id.keyLinePager);
        showListView = (ListView) view.findViewById(R.id.key_line_list_view);
        keyLineModelList = new LinkedList<>();
        //设置ListView的Adapter

        listViewAdapter = new KeyLineListViewAdapter(getActivity(), keyLineModelList);
        showListView.setAdapter(listViewAdapter);

        showLinePagerAdapter = new KeyLineFragmentAdapter(getChildFragmentManager());
        showLinePager.setAdapter(showLinePagerAdapter);
        showLinePager.setCurrentItem(500);
        int currentItem = showLinePager.getCurrentItem();
        Calendar selectCalendar = KeyLineUtils.getSelectCalendar(currentItem);
        String month = selectCalendar.get(Calendar.YEAR) +
                "-" + KeyLineUtils.LeftPad_Tow_Zero(Calendar.getInstance().get(Calendar.MONTH) + 1);
        showLinePager.setOnPageChangeListener(this);
        return view;
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Calendar calendar = KeyLineUtils.getSelectCalendar(position);
        String month = calendar.get(Calendar.YEAR)
                + "-"
                + KeyLineUtils.LeftPad_Tow_Zero(calendar.get(Calendar.MONTH) + 1);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public void getDate(Date date) {
        String currentDate = DateUtils.parseDate(date);
        showDate.setText(currentDate);
        getKeyLineListViewModel(date);
    }

    public List<KeyLineListViewModel> getKeyLineListViewModel(Date date) {
        List<KeyLineListViewModel> listViewModels = new LinkedList<>();
        String[] projection = new String[]{"bookid", "type_id", "io_choose", "fee", "createtime"};
        String selection = " createtime=? and bookid=?";
        String time = parseDate(date);

        int selectedBookId = DatabaseQueryUtils.getSelectedAccountBook();
        String[] selectionArgs = new String[]{time, selectedBookId + ""};
        Cursor cursor = DatabaseUtils.query(MyConstants.TABLE_ACCOUNT_BOOK_DETAILS, projection, selection, selectionArgs, null);
        detailsList = new LinkedList<>();
        ConsumeDetails details = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int bookid = cursor.getInt(cursor.getColumnIndex("bookid"));
                int type_id = cursor.getInt(cursor.getColumnIndex("type_id"));
                int io_choose = cursor.getInt(cursor.getColumnIndex("io_choose"));
                float fee = cursor.getFloat(cursor.getColumnIndex("fee"));
                details = new ConsumeDetails();
                String createtime = cursor.getString(cursor.getColumnIndex("createtime"));
                details.setBookid(bookid);
                details.setType_id(type_id);
                details.setIoChoose(io_choose);
                details.setFee(fee);
                details.setCreate_time(createtime);
                detailsList.add(details);
            }
            keyLineModelList.clear();
            keyLineModelList.addAll(getConsumeList(detailsList));
            listViewAdapter.notifyDataSetChanged();
        }
        return listViewModels;
    }

    private List<KeyLineListViewModel> getConsumeList(List<ConsumeDetails> consumeDetails) {
        List<KeyLineListViewModel> lists = new LinkedList<>();
        KeyLineListViewModel listViewModel = null;
        for (ConsumeDetails detail : consumeDetails) {
            listViewModel = new KeyLineListViewModel();
            int type_id = detail.getType_id();

            float fee = detail.getFee();
            int ioChoose = detail.getIoChoose();
            if (ioChoose == 1) {
                listViewModel.setCategory("收入");
            } else {
                String typeName = DatabaseQueryUtils.getNameByTypeId(type_id);
                listViewModel.setCategory(typeName);
            }
            listViewModel.setFee(fee);
            lists.add(listViewModel);
        }
        return lists;
    }

    private String parseDate(Date date) {
        String ret = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        ret = format.format(date);
        return ret;
    }

}
